//
//  VistorView.swift
//  FYBXinLang
//
//  Created by mac1 on 16/3/20.
//  Copyright © 2016年 mac1. All rights reserved.
//

import UIKit

//MARK: -按钮点击协议
// 需要继承NSObjectProtocol
protocol CZVistorViewDelegate:NSObjectProtocol {
    
    //对应的协议方法 注册
    func vistorViewWillRegister()
    
    //对应的协议方法 登录
    func vistorViewWillLogin()
    
}


class CZVistorView: UIView {
    
    //按钮点击代理
    weak var vistorViewDelegate: CZVistorViewDelegate?
    
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        // 添加到view上
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: -按钮点击事件
    //在点击事件里面调用代理方法
    func vistorViewRegister(){
//        print("注册")
        vistorViewDelegate?.vistorViewWillRegister()
    }
    
    func vistorViewLogin(){
//        print("登录")
        vistorViewDelegate?.vistorViewWillLogin()
    }
    
    //MARK: -旋转动画
    func startRotationAnimation(){
        //创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置旋转的角度
        animation.toValue = 2 * M_PI
        //设置旋转该角度需要花的时间
        animation.duration = 20
        //设置重复次数
        animation.repeatCount = MAXFLOAT
        
        //动画执行完毕是否从layer图层上移除   如果移除 则跳转tabBar不同控制器回来就不会动了 动画停止  如果不移除 则动画一直在播放 不会停止
        animation.removedOnCompletion = false
        //转轮 根据动画 做出旋转
        //forKey 不写都可以  可以根据这个值 来判断现在执行哪段动画  防止动画重复
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
        
    }
    
    //MARK: -根据不同控制器 显示不同类型
    /**
    不同页面设置不同信息
    默认为首页,首页不需要调用
    parameter imageName: 图标名称
    parameter message:   消息文字
    */
    func setupVisitorInfo(imageName:String,message:String){
        
        iconView.image = UIImage(named: imageName )
        
        messageLabel.text = message
        
        //隐藏小房子
        homeView.hidden = true
        
        //遮罩放到最下面
        sendSubviewToBack(coverView)
        
    }
    
    //MARK: - 添加到view上
    private func prepareUI(){
        
        //添加子空间
        addSubview(iconView)
        // 添加遮罩视图
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //设置图片背景颜色
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //添加Autolayot约束
        //注意使用Autolayot 约束 要关掉 Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        
        // iconView 转轮
        // iconView X方向  设置为当前view中心点
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        //iconView Y方向  设置为当前view中心点
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        
        
        
        // 房子  XY 均以转轮的中心点
        // X方向
        addConstraint( NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // Y方向
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        
        // 消息label. CenterX和iconView.CenterX重合.,宽度224 Label高度系统自定
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        // 顶部距离iconView底部16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 如果不参照其他控件toItem填nil. attribute填: NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        // 注册按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 左边和messageLabel的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 注册按钮
        // 顶部距离messageLabel 16
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        // 右边和messageLabel的右边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        // 宽度 100
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        // 高度 35
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        
        // 遮盖
        coverView.translatesAutoresizingMaskIntoConstraints = false
        // X (宽)
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        // Y 顶部和父控件顶部重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        // 底部和registerButton底部重合
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        
        
        
    }
    
    
    
    
    
    
    
    //MARK: - 控件懒加载
    
    //转轮
    private lazy var iconView :UIImageView = {
        //创建UIimage
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        //返回imageView
        
        return UIImageView(image: image)
        
    }()
    //房子
    private lazy var homeView: UIImageView = {
        //创建UIimage
        let image = UIImage(named: "visitordiscover_feed_image_house")
        //返回imageView
        
        return UIImageView(image: image)
        
    }()
    
    //文字标签
    private lazy var messageLabel:UILabel = {
        //创建label
        let label = UILabel()
        //设置内容
        label.text = "关注一些人,看看有上面惊喜"
        //设置颜色
        label.textColor = UIColor.darkGrayColor()
        //设置换行
        label.numberOfLines = 0
        //确认文字尺寸长度
        label.sizeToFit()
        
        return label
        
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //设置文字内容
        button.setTitle("注册", forState: UIControlState.Normal)
        //设置文字颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //为按钮添加点击事件
        button.addTarget(self, action: "vistorViewRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //设置文字内容
        button.setTitle("登录", forState: UIControlState.Normal)
        //设置文字颜色
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        //设置背景
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //为按钮添加点击事件
        button.addTarget(self, action: "vistorViewLogin", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    // 遮罩视图, 不要写maskView, 因为View有一个maskView属性
    private lazy var coverView :UIImageView = {
        let image = UIImage(named:  "visitordiscover_feed_mask_smallicon")
        
        return UIImageView(image: image)
        
    }()
    
}
